export default {
    data() {
        return {
            debounceTime: null,
        };
    },
    methods: {
        getWeight(cpcl) {
            let characteristicValue = this.ab2hex(cpcl);
            var trimedStr = characteristicValue.trim();
            var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
            var len = rawStr.length;
            if (len % 2 !== 0) {
                return "Illegal Format ASCII Code!";
            }
            var balanceType;
            var dataType;
            var againType;
            var resistance;
            var resistanceArr = [];
            var weight;
            var weightArr = [];
            for (var i = 0; i < len; i = i + 2) {
                if (i == 0) {
                    balanceType = rawStr.substr(i, 2);
                }
                if (i == 16) {
                    dataType = rawStr.substr(i, 2);
                }
                if (i == 18) {
                    againType = rawStr.substr(i, 2);
                }
                if (i == 6) {
                    weightArr.push(rawStr.substr(i, 2));
                }
                if (i == 8) {
                    weightArr.push(rawStr.substr(i, 2));
                }
                if (i == 10) {
                    resistanceArr.push(rawStr.substr(i, 2));
                }
                if (i == 12) {
                    resistanceArr.push(rawStr.substr(i, 2));
                }
                if (i == 14) {
                    resistanceArr.push(rawStr.substr(i, 2));
                }
            }

            console.log("准备请求数据", balanceType, dataType, againType);
            if (balanceType == "cf" && dataType == "00") {
                if (this.debounceTime != null) {
                    clearTimeout(this.debounceTime);
                }
                this.debounceTime = setTimeout(() => {
                    weight = (parseInt(weightArr.reverse().join(""), 16) / 100).toFixed(2);
                    resistance = parseInt(resistanceArr.reverse().join(""), 16);
                    console.log(weight, resistance, "阻抗的数据内容");
                    this.uploadWeight(weight, resistance);
                }, 2000);
            }
        },
        uploadWeight(weight, resistance) {
            if (weight == this.weightUploading && this.resistanceUploading == resistance) {
                uni.$emit("update", { getWeight: true });
                return;
            }
            var form = {
                resistance: resistance,
                weight: weight,
            };
            console.log("发送消息成功中", form);
            this.request.httpTokenJsonRequest("/mp-api/wx/weight/upload", form, "POST", true).then((res) => {
                if (res.code == 0) {
                    uni.$emit("update", { getWeight: true });
                    console.log("发送消息成功");
                    this.weightUploading = weight;
                    this.resistanceUploading = resistance;
                    // 上传成功之后会请求一次最新数据
                    // this.requestNewestWeight();
                    var item = res.data;
                    if (item.score.score > 0) {
                        this.isShowIntegral = true;
                        this.score = item.score.score;
                    }
                    if (item.score.message) {
                        uni.showToast({
                            title: item.score.message,
                            icon: "none",
                        });
                    }
                    this.xunzhangList = [];
                    this.xunzhangList.length = 0;
                    if (item.medalData.length > 0) {
                        item.medalData.forEach((itemMedal) => {
                            if (itemMedal.name != "" && itemMedal.name) {
                                const xunzhang = this.xunzhangList.find((v) => v.name == itemMedal.name);
                                if (!xunzhang) {
                                    this.xunzhangList.push(itemMedal);
                                }
                            }
                        });
                        if (item.score.score == 0) {
                            this.isShowXunZhang = true;
                        }
                    }
                }
            });
        },
        ab2hex(buffer) {
            let hexArr = Array.prototype.map.call(new Uint8Array(buffer), (bit) => {
                return ("00" + bit.toString(16)).slice(-2);
            });
            return hexArr.join("");
        }
    },

};
